package com.schoolCommunity.controller;

import com.schoolCommunity.pojo.ForIndex;
import com.schoolCommunity.pojo.ForIndexMessage;
import com.schoolCommunity.pojo.Information;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@RestController
public class ForIndexController {

    @Autowired
    private RestTemplate restTemplate;

    private static final String REST_URL_PREFIX = "http://SCHOOLCOMMUNITY-FORINDEX";

    //负载均衡测试用
    @GetMapping("/ForIndexHello")
    public String hello() {
        System.out.println("-------------------进入了测试--------------");
        return restTemplate.getForObject(REST_URL_PREFIX + "/hello", String.class);
    }

    //管理员功能，添加介绍
    //写入作者，地点名字，地点,图片描述即可
    @PostMapping("/InsertForIndex")
    public Information InsertForIndex(@RequestBody ForIndex forIndex) throws IOException {
        return restTemplate.postForObject(REST_URL_PREFIX + "/InsertForIndex", forIndex, Information.class);
    }

    //管理员功能，添加地点图片
    @PostMapping("/UploadPlacePicture/{forIndexId}")
    public Information UploadPicture(@RequestParam("file") MultipartFile file, @PathVariable("forIndexId") long forIndexId) throws IOException {
        return restTemplate.postForObject(REST_URL_PREFIX + "/UploadPlacePicture/" + forIndexId, file, Information.class);
    }

    //管理员功能，删除介绍
    @GetMapping("/DeleteForIndex/{id}")
    public Information DeleteForIndex(@PathVariable("id") long id) throws IOException {
        return restTemplate.getForObject(REST_URL_PREFIX + "/DeleteForIndex/" + id, Information.class);
    }

    //管理员功能，修改介绍
    @PostMapping("/UpdateForIndex")
    public Information UpdateForIndex(@RequestBody ForIndex forIndex) throws IOException {
        return restTemplate.postForObject(REST_URL_PREFIX + "/UpdateForIndex", forIndex, Information.class);
    }

    //获取所有数据
    @GetMapping("/GetAllForIndex")
    public Information GetAllForIndex() {
        return restTemplate.getForObject(REST_URL_PREFIX + "/GetAllForIndex", Information.class);
    }

    //点赞某一条介绍
    @GetMapping("/AddLovesForIndex/{id}/{memberId}")
    public Information AddLovesForIndex(@PathVariable("id") long id, @PathVariable("memberId") long memberId) {
        return restTemplate.getForObject(REST_URL_PREFIX + "/AddLovesForIndex/" + id + "/" + memberId, Information.class);
    }

    //管理员功能，删除留言
    @GetMapping("/DeleteMessage/{id}")
    public Information DeleteMessage(@PathVariable("id") long id) {
        return restTemplate.getForObject(REST_URL_PREFIX + "/DeleteMessage/" + id, Information.class);
    }

    //获取某个介绍下面的留言
    @GetMapping("/getMessageForIndex/{id}")
    public Information getMessageForIndex(@PathVariable("id") long id) {
        return restTemplate.getForObject(REST_URL_PREFIX + "/getMessageForIndex/" + id, Information.class);
    }

    //添加一条留言
    @PostMapping("/insertMessage")
    public Information insertMessage(@RequestBody ForIndexMessage forIndexMessage) {
        return restTemplate.postForObject(REST_URL_PREFIX + "/insertMessage", forIndexMessage, Information.class);
    }

    //点赞某条留言
    @GetMapping("/AddLovesForIndexMessage/{id}/{memberId}")
    public Information AddLovesForIndexMessage(@PathVariable("id") long id, @PathVariable("memberId") long memberId) {
        return restTemplate.getForObject(REST_URL_PREFIX + "/AddLovesForIndexMessage/" + id + "/" + memberId, Information.class);
    }


}
